package DEEPERsource.DEEPERsource.source.graphics;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import preprocessing.DefaultPreprocessor;
import preprocessing.SimplePreprocessor;
import util.PropertyManager;
import corpus.Text;
import corpus.ppi.CorpusFactory;

import deeper.Interaction;
import deeper.InteractionType;
import edu.uci.ics.jung.graph.ArchetypeEdge;
import edu.uci.ics.jung.graph.ArchetypeVertex;
import edu.uci.ics.jung.graph.decorators.EdgeStringer;
import edu.uci.ics.jung.graph.decorators.VertexStringer;
import edu.uci.ics.jung.visualization.FRLayout;
import edu.uci.ics.jung.visualization.Layout;
import edu.uci.ics.jung.visualization.PluggableRenderer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import graph.Keys;

public class TreeViewer extends JFrame {
	private static Log _log = LogFactory.getLog(TreeViewer.class);

	private Map<String,Interaction> data;
	private VisualizationViewer vv;
	private JLabel label = new JLabel("");

	public TreeViewer(Collection<Interaction> list, Map<String,String> pred) {
		data = new HashMap<String, Interaction>();
		final List<Item> items = new ArrayList<Item>();
		for(Interaction i : list){
			_log.debug(i.id);
			i.predClass = pred.get(i.id);
			items.add(new Item(i.id, Integer.parseInt(i.predClass), i.status.ordinal()));
			data.put(i.id, i);
		}
		Collections.sort(items);
		final JList dataList = new JList(items.toArray(new Item[] {}));
		JScrollPane scrollPane = new JScrollPane(dataList);
		final Container cp = getContentPane();
		cp.setLayout(new BorderLayout());
		cp.add(BorderLayout.WEST, scrollPane);
		cp.add(BorderLayout.NORTH, label);
		dataList.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e){
				Interaction i = data.get(items.get(dataList.getSelectedIndex()).id);
				label.setText("<html>"+i.sentence+"<br>"+i.depPath.toString().replaceAll("&lt", "<").replaceAll("&gt", ">")+"</html>");
				Layout l = new FRLayout(i.depTree);
				PluggableRenderer r = new PluggableRenderer();
				r.setEdgeStringer(new EdgeStringer() {
					public String getLabel(ArchetypeEdge e) {
						return e.getUserDatum(Keys.LABEL).toString();
					}
				});
				r.setVertexStringer(new VertexStringer() {
					public String getLabel(ArchetypeVertex v) {
						return v.getUserDatum(Keys.LABEL).toString();
					}
				});
				if (vv != null)
					cp.remove(vv);
				vv = new VisualizationViewer(l, r, new Dimension(1100, 800));
				cp.add(BorderLayout.EAST, vv);
				vv.revalidate();
			}
		});
	}

	public static void main(String[] args) {
		PropertyManager pm = new PropertyManager();
		pm.readParam("resource/props/lll.properties");
		CorpusFactory cFactory = new CorpusFactory();
		Text lll = cFactory.getCorpusFromPPIXML(pm.getParam("data"), new DefaultPreprocessor());
		lll.parse(null, "serialized", pm.getParam("parse"));
		Collection<Interaction> full = lll.interactions();
		Map<String,String> pred = new HashMap<String, String>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(pm.getParam("prediction")));
			String s;
			while((s = reader.readLine())!=null){
				String[] split = s.split("\\s+");
				pred.put(split[0],split[2]);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		TreeViewer frame = new TreeViewer(full, pred);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 800);
		frame.setVisible(true);
	}
	
	class Item implements Comparable{
		String id;
		String type;
		
		public Item(String id, int pred, int real){
			this.id = id;
			if(real == InteractionType.FALSE.ordinal()){
				if(real == pred)
					type = "TN";
				else
					type = "FP";
			}else{
				if(real == pred)
					type = "TP";
				else
					if(pred==InteractionType.FALSE.ordinal())
						type = "FN";
					else
						type = "WD";
			}			
		}
		
		public String toString(){
			return type+"  "+id;
		}

		public int compareTo(Object o) {
			return this.toString().compareTo(o.toString());
		}
	}
}
